//
//  YXLPriceTableViewCell.m
//  MyNewProject
//
//  Created by 于兴雷 on 2019/2/12.
//  Copyright © 2019年 sun. All rights reserved.
//

#import "YXLPriceTableViewCell.h"

@interface YXLPriceTableViewCell()

@property (nonatomic , weak) UIImageView *headImgView;
@property (nonatomic , weak) UILabel *stateLab;
@property (nonatomic , weak) UILabel *timeLab;
@property (nonatomic , weak) UILabel *nameLab;
@property (nonatomic , weak) UILabel *contentLab;
@property (nonatomic , weak) UIView *contentBgView;

@property (nonatomic , weak) UIImageView *endImgView;

@end

@implementation YXLPriceTableViewCell


-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        self.contentView.backgroundColor = SKRGBColor(241, 241, 241);
        
        [self initView];
    }
    
    return self;
}

-(void)initView
{
    UIImageView *headImgView = [[UIImageView alloc] init];
    headImgView.clipsToBounds = YES;
    headImgView.layer.cornerRadius = 20;
    [self.contentView addSubview:headImgView];
    self.headImgView = headImgView;

    
    UILabel *stateLab = [[UILabel alloc] init];
    stateLab.font = [UIFont systemFontOfSize:12];
    stateLab.clipsToBounds = YES;
    stateLab.textColor = [UIColor whiteColor];
    stateLab.textAlignment = NSTextAlignmentCenter;
    stateLab.layer.cornerRadius = 2;
    [self.contentView addSubview:stateLab];
    self.stateLab = stateLab;
    
    UILabel *timeLab = [UILabel new];
    timeLab.font = [UIFont systemFontOfSize:14];
    timeLab.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:timeLab];
    self.timeLab = timeLab;


    UILabel *nameLab = [[UILabel alloc] init];
    nameLab.font = [UIFont systemFontOfSize:14];
    nameLab.textColor = [UIColor grayColor];
    [self.contentView addSubview:nameLab];
    self.nameLab = nameLab;


    UIView *contentBgView = [[UIView alloc] init];
    contentBgView.backgroundColor = [UIColor whiteColor];
    contentBgView.layer.cornerRadius = 5;
    contentBgView.clipsToBounds = YES;
    [self.contentView addSubview:contentBgView];
    self.contentBgView = contentBgView;


    UILabel *contentLab = [[UILabel alloc] init];
    contentLab.textColor = [UIColor grayColor];
    contentLab.font = [UIFont systemFontOfSize:14];
    [contentBgView addSubview:contentLab];
    self.contentLab = contentLab;

    UIImageView *endImgView = [[UIImageView alloc] init];
    endImgView.contentMode = UIViewContentModeScaleAspectFit;
    endImgView.image = [UIImage imageNamed:@"auctionSuccess"];
    [self.contentView addSubview:endImgView];
    self.endImgView = endImgView;

   
}


-(void)reloaData:(YXLHistoryPriceModel *)historyPriceModel
{
    self.timeLab.text = historyPriceModel.time;
    
    [self.headImgView sd_setImageWithURL:[NSURL URLWithString:historyPriceModel.head_url] placeholderImage:[UIImage imageNamed:@"DefaultHead"]];

    self.nameLab.text = historyPriceModel.nickname;

    self.contentLab.text = [NSString stringWithFormat:@"出价:%@,获得%@贡献金,获得%@积分",historyPriceModel.price,self.sellModel.reward,self.sellModel.jifen];

    if ([historyPriceModel.is_max intValue] == 1) {
        self.stateLab.text = @"领先";
        self.stateLab.backgroundColor = SKRGBColor(255, 36, 36);

    }else{
        self.stateLab.text = @"出局";
        self.stateLab.backgroundColor = SKRGBColor(153, 153, 153);
    }
    
    [self.headImgView sd_resetLayout];
    [self.nameLab sd_resetLayout];
    [self.stateLab sd_resetLayout];
    [self.timeLab sd_resetLayout];
    [self.endImgView sd_resetLayout];
    [self.contentLab sd_resetLayout];
    [self.contentBgView sd_resetLayout];
    [self.contentView sd_resetLayout];
    if ([historyPriceModel.uid isEqualToString:[UserInfoSingleton shareInstance].userInfo.uid]) {
        [self layoutOwnPrice];
    }else{
        [self layoutOtherPrice];
    }
    self.endImgView.hidden = YES;

    if ([self.sellModel.lost_end_time integerValue] == 0 && [historyPriceModel.is_max intValue] == 1) {
        self.endImgView.hidden = NO;
    }
    
}


-(void)layoutOwnPrice
{
    
    self.timeLab.sd_layout
    .topSpaceToView(self.contentView, 10)
    .leftEqualToView(self.contentView)
    .rightEqualToView(self.contentView)
    .heightIs(20);
    
    
    self.headImgView.sd_layout
    .topSpaceToView(self.timeLab, 30)
    .rightSpaceToView(self.contentView, 20)
    .heightIs(40)
    .widthIs(40);
    
    self.nameLab.sd_layout
    .rightSpaceToView(self.headImgView, 10)
    .topSpaceToView(self.timeLab, 20)
    .heightIs(20);
    [self.nameLab setSingleLineAutoResizeWithMaxWidth:150];
    
    self.stateLab.sd_layout
    .centerXEqualToView(self.headImgView)
    .topSpaceToView(self.headImgView, -10)
    .heightIs(15)
    .widthIs(40);

    
    self.contentLab.sd_layout
    .topSpaceToView(self.contentBgView, 10)
    .leftSpaceToView(self.contentBgView, 10)
    .rightSpaceToView(self.contentBgView, 10)
    .autoHeightRatio(0);

    
    self.contentBgView.sd_layout
    .rightEqualToView(self.nameLab)
    .topSpaceToView(self.nameLab, 5)
    .leftSpaceToView(self.endImgView, 10);

    [self.contentBgView setupAutoHeightWithBottomView:self.contentLab bottomMargin:10];
    
    
    self.endImgView.sd_layout
    .centerYEqualToView(self.headImgView)
    .leftSpaceToView(self.contentView, 10)
    .heightIs(50)
    .widthIs(50);
    
    
    
    [self setupAutoHeightWithBottomView:self.contentBgView bottomMargin:20];

}


-(void)layoutOtherPrice
{
    self.timeLab.sd_layout
    .topSpaceToView(self.contentView, 10)
    .leftEqualToView(self.contentView)
    .rightEqualToView(self.contentView)
    .heightIs(20);
    
    self.headImgView.sd_layout
    .topSpaceToView(self.timeLab, 30)
    .leftSpaceToView(self.contentView, 20)
    .heightIs(40)
    .widthIs(40);

    self.nameLab.sd_layout
    .leftSpaceToView(self.headImgView, 10)
    .topSpaceToView(self.timeLab, 20)
    .heightIs(20);
     [self.nameLab setSingleLineAutoResizeWithMaxWidth:150];;

    self.stateLab.sd_layout
    .centerXEqualToView(self.headImgView)
    .topSpaceToView(self.headImgView, -10)
    .heightIs(15)
    .widthIs(40);

    
    self.contentLab.sd_layout
    .topSpaceToView(self.contentBgView, 10)
    .leftSpaceToView(self.contentBgView, 10)
    .rightSpaceToView(self.contentBgView, 10)
    .autoHeightRatio(0);

    self.contentBgView.sd_layout
    .leftEqualToView(self.nameLab)
    .topSpaceToView(self.nameLab, 5)
    .rightSpaceToView(self.endImgView, 10);


    [self.contentBgView setupAutoHeightWithBottomView:self.contentLab bottomMargin:10];



    self.endImgView.sd_layout
    .centerYEqualToView(self.headImgView)
    .rightSpaceToView(self.contentView, 10)
    .heightIs(50)
    .widthIs(50);
    
    [self setupAutoHeightWithBottomView:self.contentBgView bottomMargin:20];
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
